#include "RenderArrow.h"

RenderArrow::RenderArrow(ALLEGRO_BITMAP *arrowImage)
{
    _arrowImage = arrowImage;
    _arrowExpand = 0;
    _expandable = true;
    _invisible = false;

    _expand = false;
    _contract = false;
    _shootDirection = 0;

    _arrowHead_posX = 0;
    _arrowHead_posY = 0;

}

void RenderArrow::set_expand()
{
    if (_expandable)
    {
        _expand = true;
        _expandable = false;
    }

}

void RenderArrow::updateArrow()
{

    if (_expand)
    {
        if (_arrowExpand < 15)
        {
            _arrowExpand ++;
        }

        else if (_arrowExpand == 15)
        {
            _expand = false;
            _contract = true;
        }
    }

    else if (_contract)
    {
        if (_arrowExpand > 0)
        {
            _arrowExpand --;
        }

        else if (_arrowExpand == 0)
        {
            _expandable = true;
        }
    }

    if (_arrowExpand == 0)
    {
        _invisible = true;
    }
    else _invisible = false;

    updateArrowBitmap();

}

void RenderArrow::drawArrowImage(Man *DigDug)
{

    if ( DigDug->get_direction_x() > 0 ) //right
    {
        al_draw_bitmap(_arrowImage, DigDug->get_pos_x()+50, DigDug->get_pos_y(), 0);

        _shootDirection = 1;

        _arrowHead_posX = DigDug->get_pos_x() + (10*_arrowExpand) + 50;
        _arrowHead_posY = DigDug->get_pos_y() + 36;

    }

    else if ( DigDug->get_direction_x() < 0 ) //left
    {
        al_draw_bitmap(_arrowImage, DigDug->get_pos_x()-150, DigDug->get_pos_y(), ALLEGRO_FLIP_HORIZONTAL);

        _shootDirection = 2;

        _arrowHead_posX = DigDug->get_pos_x() - (10*_arrowExpand);
        _arrowHead_posY = DigDug->get_pos_y() + 36;

    }

    else if ( DigDug->get_direction_y() < 0 ) //up
    {
        al_draw_rotated_bitmap(_arrowImage, 50, 0, DigDug->get_pos_x(), DigDug->get_pos_y()-50, 3*ALLEGRO_PI/2 , 0);
        _shootDirection = 3;

        _arrowHead_posX = DigDug->get_pos_x() +36;
        _arrowHead_posY = DigDug->get_pos_y() - (10*_arrowExpand);
    }

    else if ( DigDug->get_direction_y() > 0 ) //down
    {
        al_draw_rotated_bitmap(_arrowImage, 0, 50, DigDug->get_pos_x(), DigDug->get_pos_y()+50, ALLEGRO_PI/2 , 0);
        _shootDirection = 4;

        _arrowHead_posX = DigDug->get_pos_x() + 14;
        _arrowHead_posY = DigDug->get_pos_y() + (10*_arrowExpand) + 50;
    }


    if ( DigDug->get_direction_x() == 0 && DigDug->get_direction_y() == 0)
    {
        if(_shootDirection == 1)
        {
            al_draw_bitmap(_arrowImage, DigDug->get_pos_x()+50, DigDug->get_pos_y(), 0);

            _arrowHead_posX = DigDug->get_pos_x() + (10*_arrowExpand) + 50;
            _arrowHead_posY = DigDug->get_pos_y() + 36;
        }

        else if (_shootDirection == 2)
        {
            al_draw_bitmap(_arrowImage, DigDug->get_pos_x()-150, DigDug->get_pos_y(), ALLEGRO_FLIP_HORIZONTAL);

            _arrowHead_posX = DigDug->get_pos_x() - (10*_arrowExpand);
            _arrowHead_posY = DigDug->get_pos_y() + 36;
        }

        else if (_shootDirection == 3)
        {
            al_draw_rotated_bitmap(_arrowImage, 50, 0, DigDug->get_pos_x(), DigDug->get_pos_y()-50, 3*ALLEGRO_PI/2 , 0);

            _arrowHead_posX = DigDug->get_pos_x() +36;
            _arrowHead_posY = DigDug->get_pos_y() - (10*_arrowExpand);
        }

        else if (_shootDirection == 4)
        {
            al_draw_rotated_bitmap(_arrowImage, 0, 50, DigDug->get_pos_x(), DigDug->get_pos_y()+50, ALLEGRO_PI/2 , 0);

            _arrowHead_posX = DigDug->get_pos_x() + 14;
            _arrowHead_posY = DigDug->get_pos_y() + (10*_arrowExpand) + 50;

        }
    }

    if (_invisible)
    {
        _arrowHead_posX = 0;
        _arrowHead_posY = 0;
    }
}

void RenderArrow::updateArrowBitmap()
{
    al_clear_to_color(al_map_rgb(0,0,0));

    al_convert_mask_to_alpha(_arrowImage, al_map_rgb(0,0,0));
    if (!_invisible)
    {
        al_draw_line(0,36,1*_arrowExpand,24+_arrowExpand,al_map_rgb(255,255,255),2);
        al_draw_line(1*_arrowExpand,24+_arrowExpand,2*_arrowExpand,48-_arrowExpand,al_map_rgb(255,255,255),2);     //arm
        al_draw_line(2*_arrowExpand,48-_arrowExpand,3*_arrowExpand,24+_arrowExpand,al_map_rgb(255,255,255),2);
        al_draw_line(3*_arrowExpand,24+_arrowExpand,4*_arrowExpand,48-_arrowExpand,al_map_rgb(255,255,255),2);
        al_draw_line(4*_arrowExpand,48-_arrowExpand,5*_arrowExpand,24+_arrowExpand,al_map_rgb(255,255,255),2);
        al_draw_line(5*_arrowExpand,24+_arrowExpand,6*_arrowExpand,48-_arrowExpand,al_map_rgb(255,255,255),2);     //arm
        al_draw_line(6*_arrowExpand,48-_arrowExpand,7*_arrowExpand,24+_arrowExpand,al_map_rgb(255,255,255),2);
        al_draw_line(7*_arrowExpand,24+_arrowExpand,8*_arrowExpand,48-_arrowExpand,al_map_rgb(255,255,255),2);
        al_draw_line(8*_arrowExpand,48-_arrowExpand,9*_arrowExpand,36,al_map_rgb(255,255,255),2);
        al_draw_line(9*_arrowExpand,36,10*_arrowExpand,36,al_map_rgb(0,0,150),2);

        al_draw_line(9*_arrowExpand,32,10*_arrowExpand,36,al_map_rgb(0,0,255),2);    //head
        al_draw_line(9*_arrowExpand,40,10*_arrowExpand,36,al_map_rgb(0,0,255),2);
    }
}

void RenderArrow::destroyArrowImage()
{
    if (_arrowImage != NULL)
    {
        al_destroy_bitmap(_arrowImage);
    }
}
